package xworker.gluon.attach.accelerometer;

import com.gluonhq.attach.accelerometer.AccelerometerService;
import com.gluonhq.attach.accelerometer.Parameters;
import org.xmeta.ActionContext;
import org.xmeta.Thing;

public class ThingAccelerometerService {
    public static void create(ActionContext actionContext){
        Thing self = actionContext.getObject("self");

        AccelerometerService as = AccelerometerService.create().orElse(null);
        if(as != null && self.getBoolean("start")){
            Parameters parameters = new Parameters(self.getDouble("frequency"), self.getBoolean("isFilteringGravity"));
            as.start(parameters);
            as.accelerationProperty().addListener((observable, oldValue, newValue) -> {
                self.doAction("onChanged", actionContext, "observable", observable, "oldValue", oldValue, "newValue", newValue);
            });

            as.accelerationProperty().addListener((observable, oldValue, newValue) -> self.doAction("onChanged", actionContext, "observable", observable, "oldValue", oldValue, "newValue", newValue));
        }
    }
}
